/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 * MIFSS  - content storage system
 * 
 *
 * @uthors: uros.kristan@gmail.com (Uroš Kristan ) Urosk.NET
 *         jernej.svigelj@gmail.com (Jernej Švigelj) 
 */
package net.monare.mifss.workers;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import net.monare.mifss.configurations.pojo.Property;
import net.monare.mifss.configurations.pojo.StorageDef;
import net.monare.mifss.exceptions.MediaConverterException;
import net.monare.mifss.exceptions.TransformationHandlerException;
import net.monare.mifss.workers.converters.BaseMediaConverter;

import org.apache.log4j.Logger;

public abstract class BaseTransformationHandler {

	public Map<String, String> getProperties() {
		return properties;
	}

    private StorageDef storageDef;


    public StorageDef getStorageDef() {
        return storageDef;
    }

    public void setStorageDef(StorageDef storageDef) {
        this.storageDef = storageDef;
    }

    public Map<String, BaseMediaConverter> getMimeTypeConverterRegistry() {
        return mimeTypeConverterRegistry;
    }

    public void setMimeTypeConverterRegistry(Map<String, BaseMediaConverter> mimeTypeConverterRegistry) {
        this.mimeTypeConverterRegistry = mimeTypeConverterRegistry;
    }

    public void setProperties(Map<String, String> properties) {
		this.properties = properties;
	}

	protected static Logger logger = Logger.getLogger(BaseTransformationHandler.class);

	private List<BaseMediaConverter> converters = new ArrayList<>();

	private Map<String, String> properties = new HashMap<String, String>();




	// media converters registry
	private Map<String, BaseMediaConverter> mimeTypeConverterRegistry = new HashMap<String, BaseMediaConverter>();

	public String getPropertyString(String key) throws MediaConverterException {



		if (!properties.containsKey(key)) {
			String msg = "No property defined for key: " + key + " in this converter";
			logger.error(msg);
			throw new MediaConverterException(msg);
		}
		return (String) properties.get(key);
	}



	protected BaseMediaConverter getConverterForMimeType(String mimeType) {

		BaseMediaConverter bmc = mimeTypeConverterRegistry.get(mimeType);



        bmc.setBaseTransformationHandler(this);

		return bmc;

	}

	public List<BaseMediaConverter> getConverters() {
		return converters;
	}

	/**
	 * This method is invoked via Transformatio manager
	 * 
	 * @throws TransformationHandlerException
	 */
	public void initialize() throws TransformationHandlerException {
		registerMimeTypeConverters();
	}

	public boolean isMimeTypeSupported(String mimeType) {

		return mimeTypeConverterRegistry.containsKey(mimeType);
	}

	private void registerMimeTypeConverters() {

		for (BaseMediaConverter mc : converters) {

			for (String mimeType : mc.getSupportedMimeTypes()) {

                mimeTypeConverterRegistry.put(mimeType, mc);

                // pass storage definition
                mc.setStorageDef(getStorageDef());
                //if(logger.isDebugEnabled())
				//logger.debug("Registering mime type: " + mimeType + " " + mc);
			}

		}
	}

	public void setConverters(List<BaseMediaConverter> converters) {
		this.converters = converters;
	}

	abstract public void transform(final String inputMimeType, final File inputFile, final File outputFile) throws TransformationHandlerException;

}
